package com.lc.pda.util;

import cn.hutool.core.date.DateUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

@UtilityClass
public class OrderNumberUtil {

    public String orderNumber (String type) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
//        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        String codeKey = String.format("%s:ORDER:CODE", type);
        String nowDay = DateUtil.today();
        String nowKey = redisTemplate.opsForValue().get(codeKey);
        String nextCode;
        if (StringUtils.isBlank(nowKey) || !nowKey.startsWith(nowDay))
        {
            nextCode = nowDay + StringUtils.leftPad("1",6,"0");
        }
        else
        {
            nextCode = nowDay + StringUtils.leftPad(String.valueOf(Integer.parseInt(StringUtils.right(nowKey, 6)) + 1), 6, "0");
        }
        redisTemplate.opsForValue().set(codeKey, nextCode);

        return type + nextCode;
    }
}
